# -*- coding:utf-8 -*-

import models, service, datetime
from .ServiceException import ServiceException
from .Case import getCase
from .Company import getCompany

def createCase(branchCompanyId, userId, _type, address, reason, insurance_time, value):
    caseId = models.Case.addCase(branchCompanyId, _type, address, reason, insurance_time, value)
    models.Action.addAction(caseId, userId, models.Action.OPERATOR_CREATE)
    return caseId

def do(caseId, userId, status, operatorType):
    models.Case.modifyCaseStatus(caseId, status)
    models.Action.addAction(caseId, userId, operatorType)

def pushCase(caseId, user):
    case = getCase(caseId)
    status = case.status
    if models.Case.STATE_CREATE == status and models.User.TYPE_CHIEF == user.type:
        do(caseId, user.id, models.Case.STATE_CHIEF_PUSH, models.Action.OPERATOR_PUSH)
        return
    elif models.User.TYPE_CHIEF == user.type:
        raise ServiceException(service.UNAUTHORIZED, '操作失败') #段长可能不属于某公司
    company = getCompany(user.company_id)
    companyType = company.type
    if models.Case.STATE_BRANCH_RECEIVE == status and models.Company.TYPE_BRANCH == companyType:
        do(caseId, user.id, models.Case.STATE_BRANCH_PUSH, models.Action.OPERATOR_PUSH)
        return
    if models.Case.STATE_ASSESS_RECEIVE == status and models.Company.TYPE_ASSESS == companyType:
        do(caseId, user.id, models.Case.STATE_ASSESS_PUSH, models.Action.OPERATOR_PUSH)
        return
    if models.Case.STATE_INSURANCE_RECEIVE == status and models.Company.TYPE_INSURANCE == companyType:
        do(caseId, user.id, models.Case.STATE_FINISHED, models.Action.OPERATOR_PUSH)
        return
    raise ServiceException(service.UNAUTHORIZED, '操作失败')

def receiveCase(caseId, user):
    case = getCase(caseId)
    status = case.status
    if models.User.TYPE_COMMON == user.type:
        company = getCompany(user.company_id)
        companyType = company.type
        if models.Case.STATE_CHIEF_PUSH == status and models.Company.TYPE_BRANCH == companyType:
            do(caseId, user.id, models.Case.STATE_BRANCH_RECEIVE, models.Action.OPERATOR_RECEIVE)
            return
        if models.Case.STATE_BRANCH_PUSH == status and models.Company.TYPE_ASSESS == companyType and models.Case.TYPE_ASSESS == case.type:
            do(caseId, user.id, models.Case.STATE_ASSESS_RECEIVE, models.Action.OPERATOR_RECEIVE)
            models.Case.setCaseAssessor(caseId, company.id)
            return
        if models.Case.STATE_BRANCH_PUSH == status and models.Company.TYPE_INSURANCE == companyType and models.Case.TYPE_SELF == case.type:
            do(caseId, user.id, models.Case.STATE_INSURANCE_RECEIVE, models.Action.OPERATOR_RECEIVE)
            return
        if models.Case.STATE_ASSESS_PUSH == status and models.Company.TYPE_INSURANCE == companyType:
            do(caseId, user.id, models.Case.STATE_INSURANCE_RECEIVE, models.Action.OPERATOR_RECEIVE)
            return
    raise ServiceException(service.UNAUTHORIZED, '操作失败')

def undoCase(caseId, user):
    case = getCase(caseId)
    status = case.status
    if models.User.TYPE_COMMON == user.type:
        company = getCompany(user.company_id)
        companyType = company.type
        if models.Case.STATE_BRANCH_RECEIVE == status and models.Company.TYPE_BRANCH == companyType:
            do(caseId, user.id, models.Case.STATE_CHIEF_PUSH, models.Action.OPERATOR_UNDO)
            return
        if models.Case.STATE_ASSESS_RECEIVE == status and models.Company.TYPE_ASSESS == companyType:
            do(caseId, user.id, models.Case.STATE_BRANCH_PUSH, models.Action.OPERATOR_UNDO)
            models.Case.setCaseAssessor(caseId, None)
            return
        if models.Case.STATE_INSURANCE_RECEIVE == status and models.Company.TYPE_INSURANCE == companyType:
            if models.Case.TYPE_SELF == case.type:
                do(caseId, user.id, models.Case.STATE_BRANCH_PUSH, models.Action.OPERATOR_UNDO)
            else:
                do(caseId, user.id, models.Case.STATE_ASSESS_PUSH, models.Action.OPERATOR_UNDO)
            return
    raise ServiceException(service.UNAUTHORIZED, '操作失败')

def returnCase(caseId, user):
    case = getCase(caseId)
    status = case.status
    if models.User.TYPE_CHIEF == user.type:
        if models.Case.STATE_CHIEF_PUSH == status or models.Case.STATE_BRANCH_RECEIVE == status or models.Case.STATE_BRANCH_PUSH == status:
            do(caseId, user.id, models.Case.STATE_CREATE, models.Action.OPERATOR_RETURN)
            return
        if models.Case.STATE_ASSESS_RECEIVE == status or models.Case.STATE_ASSESS_PUSH == status:
            do(caseId, user.id, models.Case.STATE_BRANCH_RECEIVE, models.Action.OPERATOR_RETURN)
            models.Case.setCaseAssessor(caseId, None)
            return
        if models.Case.STATE_INSURANCE_RECEIVE == status:
            if models.Case.TYPE_SELF == case.type:
                do(caseId, user.id, models.Case.STATE_BRANCH_RECEIVE, models.Action.OPERATOR_UNDO)
            else:
                do(caseId, user.id, models.Case.STATE_ASSESS_RECEIVE, models.Action.OPERATOR_UNDO)
            return
    raise ServiceException(service.UNAUTHORIZED, '操作失败')

def doModify(caseId, userId, operatorType, address, reason, insurance_time, value, content):
    models.Case.modifyCaseContent(caseId, address, reason, insurance_time, value)
    models.Action.addAction(caseId, userId, operatorType, content)

def modifyCase(caseId, user, address, reason, insurance_time, value):
    case = getCase(caseId)
    status = case.status
    try:
        insurance_time = datetime.datetime.strptime(insurance_time, "%Y-%m-%d")
    except:
        pass
    try:
        insurance_time = datetime.datetime.strptime(insurance_time, "%Y-%m-%d %H:%M:%S")
    except:
        pass
    value = int(value)
    content = ''
    if case.address != address:
        content += u'修改出险地址\n %s -> %s\n' % (case.address, address)
    if case.reason != reason:
        content += u'修改出险原因\n %s -> %s\n' % (case.reason , reason)
    if case.insurance_time != insurance_time:
        content += u'修改出险时间\n %s -> %s\n' % (str(case.insurance_time) , str(insurance_time))
    if case.value != value:
        content += u'修改估损金额\n %.2f -> %.2f\n' % (case.value / 100.0, value / 100.0)

    if '' == content:
        raise ServiceException(service.NOT_NEED_MODIFY, '无需修改')

    if models.User.TYPE_CHIEF == user.type:
        doModify(caseId, user.id, models.Action.OPERATOR_MODIFY, address, reason, insurance_time, value, content)
        return

    company = getCompany(user.company_id)
    companyType = company.type

    if models.Case.STATE_BRANCH_RECEIVE == status and models.Company.TYPE_BRANCH == companyType:
        doModify(caseId, user.id, models.Action.OPERATOR_MODIFY, address, reason, insurance_time, value, content)
        return
    if models.Case.STATE_ASSESS_RECEIVE == status and models.Company.TYPE_ASSESS == companyType:
        doModify(caseId, user.id, models.Action.OPERATOR_MODIFY, address, reason, insurance_time, value, content)
        return
    if models.Case.STATE_INSURANCE_RECEIVE == status and models.Company.TYPE_INSURANCE == companyType:
        doModify(caseId, user.id, models.Action.OPERATOR_MODIFY, address, reason, insurance_time, value, content)
        return
    raise ServiceException(service.UNAUTHORIZED, '操作失败')
